package com.yc.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.system.dto.Issure;
import com.yc.system.entity.Store;
import com.yc.system.entity.User;
import com.yc.system.mapper.*;
import com.yc.system.service.*;
import com.yc.framework.baseClass.BaseServiceImpl;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yc.framework.baseClass.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 书店 服务实现类
 * </p>
 *
 * @author 1850140986@qq.com
 * @since 2022-12-06
 */

@Service
public class StoreServiceImpl extends BaseServiceImpl<StoreMapper,Store> implements IStoreService {

    @Autowired
    public StoreMapper storeMapper;
    @Autowired
    public UserMapper userMapper;
    @Autowired
    public BookMapper bookMapper;
    @Autowired
    public BookOrderMapper bookOrderMapper;
    @Autowired
    public BookCartLikeMapper bookCartLikeMapper;
//    @Autowired
//    private IUserService iUserService;
//    @Autowired
//    private IBookService iBookService;
    @Autowired
    private IBookOrderService iBookOrderService;
    @Autowired
    private IBookCartLikeService iBookCartLikeService;

    @Override
    public Store getStoreByUserId(int userId) {
        return storeMapper.getStoreByUserId(userId);
    }

    @Override
    public R getIssureInfoByUserId(int userId) {
//        return R.ok();
        return R.ok().put("userInfo", userMapper.getUserByUserId(userId))
                .put("StoreInfo",storeMapper.getStoreByUserId(userId))
                .put("bookList",bookMapper.selectBooksByUserId(userId))
                .put("bookOrderList",bookOrderMapper.getOrderListByIssureId(userId))
                .put("count",iBookOrderService.getCountByIssureId(userId))
                .put("likeNum",iBookCartLikeService.getLikeNumByIssureId(userId))
                .put("goodNum",iBookOrderService.getGoodNumByIssureId(userId))
                ;
    }

    public Map<String,Object> getIssureAllByUserId(int userId){
        Map<String,Object> map=new HashMap<>();
        map.put("userInfo", userMapper.getUserByUserId(userId));
        map.put("StoreInfo",storeMapper.getStoreByUserId(userId));
        map.put("bookList",bookMapper.selectBooksByUserId(userId));
        map .put("bookOrderList",bookOrderMapper.getOrderListByIssureId(userId));
        map .put("count",iBookOrderService.getCountByIssureId(userId));
        map .put("likeNum",iBookCartLikeService.getLikeNumByIssureId(userId));
        map.put("goodNum",iBookOrderService.getGoodNumByIssureId(userId));
        return map;
    }

    /**
    * 分页查询实体列表
    * @param model 查询参数
    * @return 查询结果
    */
    @Override
    public R list( Store model){
        IPage<Store> page = new Page<>(model.getPageNum(),model.getPageSize());
        QueryWrapper<Store> wrapper = new QueryWrapper<>();
         initCondition(wrapper);
        setParam(wrapper,model);
         IPage<Store> px = page(page,wrapper);
        return R.ok().put("data",px.getRecords()).put("total", px.getTotal());
    }

    @Override
    public R getStoresByPageRandom(Store findByPage) {
        IPage<Store> page = new Page<>(findByPage.getPageNum() ,findByPage.getPageSize());
			QueryWrapper<Store> wrapper = new QueryWrapper<>();
//			wrapper.like("product_name","v");
			return R.ok().put("isserList",storeMapper.getStoresByPageRandom(page, wrapper, 1));

    }

    @Override
    public R getStoresAllByPage(Store findByPage) {
        IPage<Store> page = new Page<>(findByPage.getPageNum() ,findByPage.getPageSize());
        QueryWrapper<Store> wrapper = new QueryWrapper<>();
        List<Store> storeList = storeMapper.getStoresByPageRandom(page, wrapper, 1);
        List<Map<String,Object>> issureAList=new ArrayList<>();
        for(Store store: storeList){
            issureAList.add(getIssureAllByUserId(store.getUserId()));
        }
        return R.ok().put("isserAllList",issureAList);
    }



    /**
    * 根据主键删除实体对象，返回值封装成R，若成功则会把要删除的主键计入返回的R
    *
    * @param id 主键
    * @return 删除是否成功
   
    @Override
    public R removeModelById(String id){
        Store model=new Store();
        model.setId(id);
        return removeModelById(model);
    }
 */
    public  void setParam(QueryWrapper<Store> wrapper, Store model) {
        if (model == null) {
        return;
        }
       // wrapper.eq("deleted", false);
    }
}
